package com.itcast.controller;

import com.itcast.annotation.Log;
import com.itcast.pojo.*;
import com.itcast.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;

    /*
        //分页查询
        //@RequestParam(defaultValue = "1")    给参数设置默认值*********
    @GetMapping
    public Result pageQuery(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer pageSize){
        PageBean pageBean = empService.pageQuery(page,pageSize);
        //{}可以读取后面的参数
        log.info("查询员工信息, page={}, pageSize={}", page, pageSize);
        return Result.success(pageBean);
    }*/

    /**
     * 分页查询(条件)
     * @param param
     * @return
     */
    @GetMapping
    public Result pageQuery(EmpQueryParam param){
        PageBean pageBean = empService.pageQuery(param);
        log.info("查询员工信息, {}", param);
        return Result.success(pageBean);
    }

    /**
     * 新增员工(并添加日志到emp_log表)
     * @param empDto
     * @return
     */
    @Log
    @PostMapping
    public Result add(@RequestBody EmpDto empDto) throws Exception {
        log.info("新增员工信息, {}", empDto);
        empService.add(empDto);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     */
    @Log
    @DeleteMapping//@RequestParam注解可以转换相同名称的不同类型的数据(数组-->集合)
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除id为{}的员工",ids);
        empService.delete(ids);
        return Result.success();
    }

    /**
     * 根据Id查询(回显)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询id为{}的员工的信息",id);
        EmpDto empDto = empService.getById(id);
        return Result.success(empDto);
    }

    /**
     * 修改员工
     * @param empDto
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody EmpDto empDto){
        log.info("修改员工的信息，{}",empDto);
        empService.update(empDto);
        return Result.success();
    }

    /**
     * 查询所有员工
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<Emp> empList = empService.list();
        log.info("查询所有员工{}",empList);
        return Result.success(empList);
    }

    /**
     * 修改用户密码
     * @param changePwd
     * @return
     */
    @PutMapping("/changePassword")
    public Result changePwd(@RequestBody ChangePwd changePwd){
        empService.changePwd(changePwd);
        return Result.success();
    }
}
